home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FAQ.SWG / 0035_Tone generator tutorial.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  12KB  |  295 lines

  1. {
  2.  
  3. Two ways to create dial tones: digital sample for each tone, or generate tone
  4. with a digital to analog convertor. The MCI mmSystem uses MS Windows 3.1 high
  5. resolution timer. The following demonstrate other ways to generate tones: }
  6.  
  7. program PlayTone;
  8. { Author: John Howard }
  9. uses WinProcs;
  10. (* Comments from Don Phillip Gibson, CompuServe [75725,1752] are enclosed in
  11.    star brackets *)
  12. const Magic : integer=376;
  13.       Tempo : integer = 120;
  14.       NoteType : integer = 4;
  15. (*
  16.         Magic is used as a multiplier to determine the duration of a
  17.         note.  The Windows API documentation for setVoiceSound
  18.         indicates that duration should be a straight forward
  19.         calculation of yea-so-many clock ticks.  It just isn't so.
  20.         Brute force experimentation found 376.  It seems to work fine
  21.         regardless of processor speed or whatever.  I've tested on
  22.         386/33, 386/16, and 8088/4.7 machines -- they all work.  Let
  23.         me tell you, it was sure fun setting up and running Windows on
  24.         that 8088/4.7 CGA equipment.
  25.  
  26.     The actual tone production routines follow.  If you've explored
  27.     the API music functions at all, you may wonder why I'm using
  28.     setVoiceSound instead of setVoiceNote.  setVoiceNote seems, on the
  29.     surface, to be the automatic way to write these sorts of things,
  30.     but it just doesn't work very well.  Whole notes and half notes
  31.     are incorrectly produced, dots are impossible, and the nicety of
  32.     having legato is gone.  setVoiceSound works much better, though it
  33.     does require that you calculate a duration rather than just
  34.     specifying tempo and length.
  35.  
  36. Windows wants the fractional and integer portions of the frequency
  37. stuffed respectively into the low and high words of a long integer.
  38.  
  39. setVoiceSound doesn't provide for a rest.  Instead, I've plugged an
  40. impossibly high pitch into the [0] slot of that array.  It's
  41. presumably playing, but you shouldn't hear it.
  42. *)
  43. var   Pitch : array[0..84] of LongInt;
  44.       Herz  : array[0..11] of Real;
  45.       Tone  : integer;              { index }
  46.  
  47.  function Duration(Tempo, NoteType : integer) : integer;
  48.  var Temp : real;
  49.  begin
  50.    Temp := 60 / Tempo * Magic * 4 / NoteType;
  51.    Duration := trunc(Temp);
  52.  end;
  53.  
  54. BEGIN  {main}
  55. Pitch[0] := MakeLong(0,20000);
  56. Tone := 0;                          { example tone index }
  57. Herz[Tone] := 523.25;               { note 'C' white frequency }
  58. Pitch[Tone+1] := MakeLong(trunc(frac(Herz[Tone])),trunc(int(Herz[Tone])));
  59. setVoiceSound(1,Pitch[Tone+1],Duration(Tempo,NoteType) * 7 div 8 );
  60.                                     {1. Accept note number}
  61. setVoiceSound(1,Pitch[0],Duration(Tempo,NoteType) * 1 div 8 );
  62.                                     {2. Sound off, zero means silence}
  63.                                     {3. Translate note into 'frequency'}
  64.                                     {4. Setup timer chip}
  65.                                     {5. Setup frequency}
  66.                                     {6. Sound on}
  67.                                     {7. Setup note length delays}
  68. (*
  69. I don't know why I've got to send one last 'empty' note to the
  70. voice queue, but without it, the last real note doesn't get played.
  71. That's the purpose of the next statement.
  72. *)
  73. setVoiceSound(1,0,1);
  74. setVoiceThreshold(1,0);
  75. StartSound;
  76. repeat until GetThresholdStatus = 1;
  77. CloseSound;
  78. END. {main}
  79.  
  80.             1-800-451-6644 Philips/Signetics BBS Filename: DTMF.ZIP
  81. { snippet follows }
  82. ; The following code uses both timers in an 80c31 to generate
  83. ; DTMF tones, and signalling tones such as BUSY, RING-BACK, etc.
  84. ; This file also contains the connections for a very crude 2 bit
  85. ; per tone A\D converter wich uses 4 bits of P1 and a low pass filter.
  86. ; Compensation for twist isn't included but could be handled by
  87. ; playing with the hi and low tone dac values and the summing amp
  88. ; input resistors.
  89. ;
  90. ; If this code is used in any application I only request that
  91. ; credit be given to me: 
  92. ;        Bert Rinne
  93. ;        Advanced Logic Systems Inc.
  94. ;        13 Twin Meadows Dr.
  95. ;        Hudson N.H. 03051
  96. ;        (C) 1993
  97. ;   *** Another method of tone generation is documented in the
  98. ;        'YUCK' Zilog Z8 microcontrollers book circa 1991 using the
  99. ;        Super8. It is a waveform synthesis model using a 8Khz sampled
  100. ;        data system with 1 timer and an 8 bit dac. If anyone should
  101. ;        adapt the technique to the '51/'31 family please post the
  102. ;        code on this BBS in return for the info.
  103. ;                
  104. ;                                        Bert Rinne 12/93
  105. SIGNAL_LOW      equ     021h    ;2 bytes for signal fLOW timer
  106. SIGNAL_HIGH     equ     023h    ;2 bytes for signal fHIGH timer
  107. fHIGH           equ     025h    ; frequency high
  108. fLOW            equ     026h    ; frequency low
  109. CNT_10mS        equ     027h    ; decrement 10 millisecond used as a timer
  110. ;   HARDWARE CONFIGURATION                     ____/\/\/\___
  111. ;--------,                                     |           |
  112. ; 8031   |      47k                            +-----][----|
  113. ;   P1.0 |-----/\/\/\------,                   |           |
  114. ;        |      12k        +--/\/\/\--,        |    \      |
  115. ;   P1.1 |-----/\/\/\------'          |        |   |   \   |
  116. ;        |                            +---][---+---|-    \_|____||___ 
  117. ;        |      47k                   |            |     /      ||
  118. ;   P1.2 |-----/\/\/\------,          |         ,--|+  /
  119. ;        |      12k        |--/\/\/\--'         |  |/
  120. ;   P1.3 |-----/\/\/\------'                    |
  121. ;--------'                                      +2.5V
  122. ; the dac uses 3 bits per channel internal to the mpu and
  123. ; counts as follows 000 001 010 011 100 101 110 111
  124. ; using bit 2 as the sign and complementing bits 0 & 1 we
  125. ; get 000 001 010 011 111 110 101 100 and
  126. ; output 00 01 10 11 11 10 01 00
  127. ; The above was coupled to a 100mw amp and used to dial the phone
  128. ; via acoustical coupling (held phone near speaker) with no errors.
  129. ;
  130. ; Funky DAC courtesy of Don Lancaster's wonderful book - 
  131. ; The CMOS Cookbook, Howard Sams Inc.
  132. ;
  133. ;       DIALING TONES (dtmf)
  134. ;
  135. ;                       1209    1336    1477    1633
  136. ;
  137. ;               697       1       2       3       A
  138. ;
  139. ;               770       4       5       6       B
  140. ;
  141. ;               852       7       8       9       C
  142. ;
  143. ;               941       *       0       #       D 
  144. ;
  145. ;   Using the formula  f = -(((osc/12)/bits-per-cycle)/desired-freq.)
  146. ;
  147. ;   thus at 11.059 MHz clock and 8 bits per cycle:
  148. ;
  149. ;     desired        timer value     actual freq
  150. ;       697     =       -165            698.2
  151. ;       770     =       -150            768
  152. ;       852     =       -135            853.3
  153. ;       941     =       -122            944
  154. ;
  155. ;       1209    =       -95             1212.6
  156. ;       1336    =       -86             1339.5
  157. ;       1477    =       -78             1476.9
  158. ;       1633    =       -71             1622.5
  159. ; Low frequency  low byte
  160. F697    equ     LOW(-165)
  161. F770    equ     LOW(-150)
  162. F852    equ     LOW(-135)
  163. F941    equ     LOW(-122)
  164. ; High frequency  low byte
  165. F1209   equ     LOW(-95)
  166. F1336   equ     LOW(-86)
  167. F1477   equ     LOW(-78)
  168. F1633   equ     LOW(-71)
  169. ;       Tone time on    >40 mS          Bell spec minimum so we
  170. ;       Tone time off   >40 mS          will use 100mS on/70 mS off
  171. ;
  172. ;       SIGNAL TONES
  173. ; NOTE *** the signal tone values are adjusted by LOAD_DTMF:
  174. ; to compensate for latency time while re-loading the timers.
  175. ; The latency time is 2 us. and is critical for 
  176. ; valid signal tone frequencies.
  177. ;
  178. ;                       Low  High (Hz)
  179. ;       Dial tone       350  440         steady tone            -13dBm
  180. ;       ring-back       440  480         2 sec on/ 4 sec off    -19dBm
  181. ;       busy            480  620        .5 sec on/.5 sec off    -24dBm
  182. ; NOTE: *******
  183. ;        Although the following are not implemented, they are valid
  184. ;        signal tones:
  185. ;
  186. ;        Reorder:        480  620        .25 sec on/.25 sec off        -24dBm
  187. ;
  188. ;        Partial dial tone
  189. ;                        480                steady tone                -17dBm
  190. ;
  191. ;        Auto credit call prompting:
  192. ;                        941  1477        940 mSEC                -10dBm/freq.
  193. ;        followed by        440  350        exponentially decaying from -10dBm
  194. ;                                        @ a time constant of 200mSec.
  195. ;
  196. ;        Reference:        Mitel Semiconductor Data book circa '86-'87.
  197. ;                        "Credit must be given where due."
  198. ;
  199. ;        Pulse dialing can be accomplished very easily using the timers
  200. ;        and if you're reading this you can figure it out.
  201. ;
  202. ;     desired        timer value     actual freq
  203. ;       350             -329            350
  204. ;       440             -262            439.7
  205. ;       480             -240            480
  206. ;       620             -186            619
  207. F350    equ     -329
  208. F440    equ     -262
  209. F480    equ     -240
  210. F620    equ     -186
  211. TONE_TBL:
  212. ; 0
  213.         db      F941                    ;fLOW  = 941
  214.         db      F1336                   ;fHIGH = 1336
  215.         db      76                      ;76 * fLOW intrs = 10mS
  216.         db      0
  217. ; 1
  218.         db      F697                    ;fLOW  = 697  
  219.         db      F1209                   ;fHIGH = 1209
  220.         db      56                      ;56 * fLOW intrs = 10 mS
  221.         db      0
  222. ; 2
  223.         db      F697                    ;fLOW  = 697  
  224.         db      F1336                   ;fHIGH = 1336 
  225.         db      56
  226.         db      0
  227. ; 3
  228.         db      F697                    ;fLOW  = 697  
  229.         db      F1477                   ;fHIGH = 1477 
  230.         db      56
  231.         db      0
  232. ; 4
  233.         db      F770                    ;fLOW  = 770  
  234.         db      F1209                   ;fHIGH = 1209
  235.         db      62
  236.         db      0
  237. ; 5
  238.         db      F770                    ;fLOW  = 770  
  239.         db      F1336                   ;fHIGH = 1336
  240.         db      62
  241.         db      0
  242. ; 6
  243.         db      F770                    ;fLOW  = 770  
  244.         db      F1477                   ;fHIGH = 1477 
  245.         db      62
  246.         db      0
  247. ; 7
  248.         db      F852                    ;fLOW  = 852  
  249.         db      F1209                   ;fHIGH = 1209
  250.         db      68
  251.         db      0
  252. ; 8
  253.         db      F852                    ;fLOW  = 852    (853)
  254.         db      F1336                   ;fHIGH = 1336   (1340)
  255.         db      68
  256.         db      0
  257. ; 9
  258.         db      F852                    ;fLOW  = 852    (853)
  259.         db      F1477                   ;fHIGH = 1477   (1477)
  260.         db      68
  261.         db      0
  262. ; A
  263.         db      F697                    ;fLOW  = 697    (698)
  264.         db      F1633                   ;fHIGH = 1633   (1622)
  265.         db      56
  266.         db      0
  267.  
  268. ; B
  269.         db      F770                    ;fLOW  = 770    (768)
  270.         db      F1633                   ;fHIGH = 1633   (1622)
  271.         db      62
  272.         db      0
  273. ; C
  274.         db      F852                    ;fLOW  = 852    (853)
  275.         db      F1633                   ;fHIGH = 1633   (1622)
  276.         db      68
  277.         db      0
  278. ; D
  279.         db      F941                    ;fLOW  = 941    (944)
  280.         db      F1633                   ;fHIGH = 1633   (1622)
  281.         db      76
  282.         db      0
  283. ; *
  284.         db      F941                    ;fLOW  = 941    (944)
  285.         db      F1209                   ;fHIGH = 1209   (1213)
  286.         db      76
  287.         db      0
  288. ; #
  289.         db      F941                    ;fLOW  = 941    (944)
  290.         db      F1477                   ;fHIGH = 1477   (1477)
  291.         db      76
  292.         db      0
  293.  
  294.